package com.mzy.basics.utils.Date;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author mzy
 * @Date 2022/4/29 15:29
 * @Version 1.0
 */
public class DateTrans {
    public static void main(String[] args) {
        String time="2021-09-16 10:01:05";
        String timeOne = "2021-09-16 11:03:10";
        System.out.println(getDateSubSeconds(time,timeOne));
        System.out.println(getDateSubMinutes(time,timeOne));
    }

    public static double getDateSubMinutes(String time, String timeOne) {
        //计算相差分钟数，并保留两位小数
        long seconds = getDateSubSeconds(time, timeOne);
        BigDecimal b = new BigDecimal(seconds / 60.0D);
        return b.setScale(2, RoundingMode.HALF_UP).doubleValue();
    }

    public static long getDateSubSeconds(String time, String timeOne) {
        //计算秒数
        LocalDateTime localDateTime = LocalDateTime.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        LocalDateTime localDateTimeOne = LocalDateTime.parse(timeOne, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return Duration.between(localDateTime, localDateTimeOne).toMillis() / 1000;
    }
}
